package com.tan.mypark.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.tan.mypark.R;

import java.util.Map;

public class DisplaySideDialog {
    private Context mContext;
    private OnDisplaySideSelectedListener mListener;
    private int mCurrentDisplayId;
    private Map<Integer, String> mCurrentMapping;

    // 回调接口，用于返回选择结果
    public interface OnDisplaySideSelectedListener {
        void onSideSelected(String side);
        void onSaveMapping(Map<Integer, String> mapping, String side);
    }

    public DisplaySideDialog(Context context, int currentDisplayId, 
                            Map<Integer, String> currentMapping, 
                            OnDisplaySideSelectedListener listener) {
        this.mContext = context;
        this.mCurrentDisplayId = currentDisplayId;
        this.mCurrentMapping = currentMapping;
        this.mListener = listener;
    }

    // 显示对话框
    public void show() {
        if (mContext == null || !(mContext instanceof Activity) || ((Activity) mContext).isFinishing()) {
            return;
        }

        // 加载自定义布局
        View dialogView = LayoutInflater.from(mContext).inflate(R.layout.dialog_display_side, null);
        
        // 初始化控件
        TextView titleTv = dialogView.findViewById(R.id.dialog_title);
        TextView messageTv = dialogView.findViewById(R.id.dialog_message);
        ImageView infoIv = dialogView.findViewById(R.id.iv_info);
        Button aSideBtn = dialogView.findViewById(R.id.btn_a_side);
        Button bSideBtn = dialogView.findViewById(R.id.btn_b_side);

        // 设置内容
        titleTv.setText("配置屏幕显示面");
//        messageTv.setText("当前屏幕DisplayId：" + mCurrentDisplayId + "\n请选择此屏幕对应的显示面：");
        messageTv.setText("请手动选择此屏幕对应的显示面（A面 or B面）：");

        // 创建对话框
        AlertDialog dialog = new AlertDialog
                .Builder(mContext, R.style.CustomDialogStyle)
                .setView(dialogView)
                .setCancelable(false)
                .create();

        // 处理A面选择
        aSideBtn.setOnClickListener(v -> {
            handleSelection("A", dialog);
        });

        // 处理B面选择
        bSideBtn.setOnClickListener(v -> {
            handleSelection("B", dialog);
        });

        // 显示对话框并设置动画
        if (dialog.getWindow() != null) {
//            dialog.getWindow().setWindowAnimations(R.style.DialogAnimation);
            // 设置对话框宽度为屏幕宽度的85%
            WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
            params.width = (int) (mContext.getResources().getDisplayMetrics().widthPixels * 0.85);
//            params.height=(int) (mContext.getResources().getDisplayMetrics().heightPixels * 0.85);
            dialog.getWindow().setAttributes(params);
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }

        dialog.show();
    }

    // 处理选择逻辑
    private void handleSelection(String side, AlertDialog dialog) {
        if (mListener != null) {
            // 关键修复：将当前DisplayId和选择的Side加入映射表
            mCurrentMapping.put(mCurrentDisplayId, side);
            // 保存更新后的映射表
            mListener.onSaveMapping(mCurrentMapping, side);
            mListener.onSideSelected(side);
        }
        dialog.dismiss();
    }
}
    